package com.nicolasprof.profgallery.ui {
	import com.nicolasprof.profgallery.Engine;
	import com.nicolasprof.profgallery.assets.TooltipBG;

	import flash.display.*;
	import flash.text.*;

	/**
	 * @author Nicolas Prof
	 */
	public class ToolTip extends Sprite {

		private var _holder : Sprite;
		private var _width : Number;
		private var _height : Number;
		private var _format : TextFormat;
		private var _tf : TextField;
		private var _padding : int;
		private var _bg : TooltipBG;
		private var _marginX : int;
		private var _marginY : int;

		
		public function ToolTip($text : String) {
			//INIT
			var config : XML = Engine.CONFIG_XML;
			_holder = new Sprite();
			_bg = new TooltipBG();
			_width = 0;
			_height = 0;
			_padding = int(config.tip.@padding);
			_marginX = int(config.tip.@marginX);
			_marginY = int(config.tip.@marginY);
			_format = new TextFormat();
			_format.font = "Arial";
			_format.kerning = true;
			_format.size = 8;
			_format.color = uint(config.tip.@textColor);
			_tf  = new TextField();
			_tf.defaultTextFormat = _format;
			_tf.autoSize = TextFieldAutoSize.LEFT;
			_tf.multiline = false;
			_tf.wordWrap = false;
			_tf.text = "";
			_tf.x = _padding + _marginX;
			_tf.y = _padding + _marginY;
			_bg.x = _marginX;
			_bg.y = _marginY;
			
			
			//DISPLAY LIST
			_holder.addChild(_bg);
			_holder.addChild(_tf);
			addChild(_holder);
			
			//ACTIONS
			this.mouseEnabled = false;
			this.mouseChildren = false;
			
			_tf.text = $text.toUpperCase();
			_width = 2*_padding + _tf.width;
			_height = 2 * _padding + _tf.height;
			
			draw();
		}
		private function draw() : void {
			_bg.width = _width;
			_bg.height = _height;
		}
		/**
		 * Getters / Setters
		 */
		override public function set x($value : Number) : void {
			var offset : int = $value + _width + _marginX;
			if(offset > this.stage.stageWidth) {
				offset = this.stage.stageWidth - offset;
			} else {
				offset = 0;
			}
			super.x = $value + offset;
		}
		override public function set y($value : Number) : void {
			var offset : int = $value + _height + _marginY;
			if(offset > this.stage.stageHeight) {
				offset = this.stage.stageHeight - offset;
			} else {
				offset = 0;
			}
			super.y = $value + offset;
		}
	}
}
